package net.xiaosaguo.mymall.order.config;

import com.zaxxer.hikari.HikariDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

/**
 * description: Seata 配置，代理数据源
 *
 * @author xiaosaguo
 * @date 2020/07/16
 */
@Configuration
public class SeataConfig {

    /**
     * 查看默认配置，DataSourceAutoConfiguration，@Import({ DataSourceConfiguration.Hikari.class,
     */
    @Bean
    public DataSource dataSource(DataSourceProperties dataSourceProperties) {
        // properties.initializeDataSourceBuilder().type(type).build();
        HikariDataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
        if (StringUtils.hasText(dataSourceProperties.getName())) {
            dataSource.setPoolName(dataSourceProperties.getName());
        }
        return new DataSourceProxy(dataSource);
    }
}
